﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CTEM.Controllers.BUS;
using CTEM.Models;
using CTEM.Controllers.Global;
using DevExpress.XtraEditors;
using CTEM.Controllers.Global;
using System.Diagnostics;

namespace CTEM
{
    public partial class LoginForm : Form
    {
        EmployeeBUS EmployeeBUS = new EmployeeBUS();
        RightBUS RightBUS = new RightBUS();
        private EmployerDTO _currentUser;
        public static bool IsLogout;
        public bool isRememPass;

        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            StackFrame CurrentStackFrame = new StackFrame();
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            if (username.Equals(Constants.EMPTY_TEXT) || password.Equals(Constants.EMPTY_TEXT))
            {
                XtraMessageBox.Show(Constants.VALIDATE_USERNAME_PASSWORD, Constants.CONFIRM, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUsername.Focus();
                return;
            }

            if (username.Equals(Constants.USERNAME_MANAGE_DB))
            {
                if (DataProvider.Encrypt(password).Equals(Constants.PASSWORD_DATABASE))
                {
                    using (DBInfoForm dbInfoForm = new DBInfoForm())
                    {
                        dbInfoForm.ShowDialog();
                    }
                    txtUsername.Text = Constants.EMPTY_TEXT;
                    txtPassword.Text = Constants.EMPTY_TEXT;
                    txtUsername.Focus();
                    return;
                }
            }
            _currentUser = EmployeeBUS.CheckLogin(username, password);
            if (_currentUser != null)
            {
                Global.CurrentUser = _currentUser;
                Global.Rights = RightBUS.LoadAllRight();
                Hide();
                using (MainForm mainForm = new MainForm())
                {
                    mainForm.ShowDialog();
                }
                if (IsLogout)
                {
                    Show();
                    IsLogout = false;
                    if (!isRememPass)
                    {
                        txtPassword.Text = Constants.EMPTY_TEXT;
                    }
                }
                else
                {
                    Close();
                }
            }
            else
            {
                XtraMessageBox.Show(Constants.INVALID_USERNAME_PASSWORD, Constants.CONFIRM, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                string msn = Global.LogMessage(CurrentStackFrame.GetFileName(), CurrentStackFrame.GetMethod().Name, Constants.INVALID_USERNAME_PASSWORD);
                Logger.WriteLog(LogLevel.ERROR, msn);
                txtUsername.Focus();
            }
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            DatabaseInfo dbInfo = new DatabaseInfo();
            dbInfo.LoadInfo();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtUsername_Leave(object sender, EventArgs e)
        
        {
            if (txtPassword.Focused)
            {
                if (txtUsername.Text.Equals(Constants.EMPTY_TEXT))
                {
                    XtraMessageBox.Show(Constants.VALIDATE_USERNAME, Constants.CONFIRM, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtUsername.Focus();
                }
            }          
        }

        private void txtPassword_Leave(object sender, EventArgs e)
        {
            if (btnLogin.Focused)
            {
                if (txtPassword.Text.Equals(Constants.EMPTY_TEXT))
                {
                    XtraMessageBox.Show(Constants.VALIDATE_PASSWORD, Constants.CONFIRM, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtPassword.Focus();
                }
            }
        }

        private void ckeRememPass_CheckedChanged(object sender, EventArgs e)
        {
            isRememPass = ckeRememPass.Checked;
        }
    }
}